/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models;
import Entity.Customers;
import dbConnect.jdbcConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class adapterCustomers {

    Connection con = null;

    public adapterCustomers() throws Exception {
        con = jdbcConnection.getConnection();
    }

    public ArrayList<Customers> getAllCustomers() {
        try {
            ArrayList<Customers> listCus = new ArrayList<Customers>();
            PreparedStatement pr = con.prepareStatement("select*from Customers");
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Customers cus = new Customers();
                cus.setCodeCustomer(rs.getInt(1));
                cus.setUserCustomer(rs.getString(2));
                cus.setPasswourds(rs.getString(3));
                cus.setFullName(rs.getString(4));
                cus.setGender(rs.getBoolean(5));
                cus.setDob(rs.getString(6));
                cus.setPhoneNumber(rs.getString(7));
                cus.setEmail(rs.getString(8));
                cus.setAddress(rs.getString(9));
                listCus.add(cus);

            }
            return listCus;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    //method search admin

    public ArrayList<Customers> searchCustomers(String name) {
        try {
            ArrayList<Customers> listCus = new ArrayList<Customers>();
            PreparedStatement pr = con.prepareStatement("select*from Customers where userCustomer like ?");
            pr.setString(1,"%"+ name+"%");
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Customers cus = new Customers();
                cus.setCodeCustomer(rs.getInt(1));
                cus.setUserCustomer(rs.getString(2));
                cus.setPasswourds(rs.getString(3));
                cus.setFullName(rs.getString(4));
                cus.setGender(rs.getBoolean(5));
                cus.setDob(rs.getString(6));
                cus.setPhoneNumber(rs.getString(7));
                cus.setEmail(rs.getString(8));
                cus.setAddress(rs.getString(9));
                listCus.add(cus);

            }
            return listCus;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
     
    public ArrayList<Customers> searchCodeCustomers(int code) {
        try {
            ArrayList<Customers> listCus = new ArrayList<Customers>();
            PreparedStatement pr = con.prepareStatement("select * from Customers where codeCustomer = ?");
            pr.setInt(1, code);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Customers cus = new Customers();
                cus.setCodeCustomer(rs.getInt(1));
                cus.setUserCustomer(rs.getString(2));
                cus.setPasswourds(rs.getString(3));
                cus.setFullName(rs.getString(4));
                cus.setGender(rs.getBoolean(5));
                cus.setDob(rs.getString(6));
                cus.setPhoneNumber(rs.getString(7));
                cus.setEmail(rs.getString(8));
                cus.setAddress(rs.getString(9));
                listCus.add(cus);

            }
            return listCus;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
 // tim kiem theo ngay sinh
    public ArrayList<Customers> searchDobCustomers(String date) {
        try {
//            ArrayList<Customers> listCus = new ArrayList<Customers>();
//            PreparedStatement pr = con.prepareStatement("select count(dob) as tong from Customers where dob like ?");
//            pr.setString(1, date);
//            ResultSet rs = pr.executeQuery();
//            while (rs.next()) {
//                Customers cus = new Customers();
//                cus.setTong(rs.getString(1));
//                listCus.add(cus);
//            }
             ArrayList<Customers> listCus = new ArrayList<Customers>();
            PreparedStatement pr = con.prepareStatement("select * from Customers where dob like ?");
            pr.setString(1,"%"+ date+"%");
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Customers cus = new Customers();
               cus.setCodeCustomer(rs.getInt("codeCustomer")) ;
               cus.setUserCustomer(rs.getString("userCustomer"));
               cus.setAddress(rs.getString("address"));
               cus.setDob(rs.getString("dob"));
               cus.setEmail(rs.getString("email"));
               cus.setFullName(rs.getString("fullName"));
               cus.setGender(rs.getBoolean("gender"));
               cus.setPasswourds(rs.getString("passwourds"));
               cus.setPhoneNumber(rs.getString("phoneNumber"));
                listCus.add(cus);
            }
            return listCus;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
//method insert admin

    public boolean insertCustomers(String userName, String passwourd, String fullName,boolean gender,String dob, String phone,String email,String address) {
        try {
            PreparedStatement pr = con.prepareStatement("insert into Customers values(?,?,?,?,?,?,?,?)");
            pr.setString(1, userName);
            pr.setString(2, passwourd);
            pr.setString(3, fullName);
            pr.setBoolean(4, gender);
            pr.setString(5,dob);
            pr.setString(6, phone);
            pr.setString(7,email);
            pr.setString(8,address);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //method update admin

    public boolean updateCustomer(String userName, String passwourd, String fullName,boolean gender,String dob, String phone,String email,String address,int code) {
        try {
            PreparedStatement pr = con.prepareStatement("update Customers set userCustomer=?,passwourds=?,fullName=?,gender=?,dob=?,phoneNumber=?,email=?,address=? where codeCustomer like ?");
            pr.setString(1,userName);
            pr.setString(2, passwourd);
            pr.setString(3, fullName);
            pr.setBoolean(4, gender);
            pr.setString(5, dob);
            pr.setString(6, phone);
            pr.setString(7, email);
            pr.setString(8,address);
            pr.setInt(9, code);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //method delete admin

    public boolean deleteCustomer(int code) {
        try {
            PreparedStatement pr = con.prepareStatement("delete Customers  where codeCustomer=?");
            pr.setInt(1, code);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //

    public boolean kiemTra(String nameHa, String passHa) {
        try {
            PreparedStatement pr = con.prepareStatement("select userCustomer,passwourds from Customers where userCustomer=? and passwourds=?");
            pr.setString(1, nameHa);
            pr.setString(2, passHa);
            ResultSet rs=pr.executeQuery();
            if (rs.next()) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
}
